import componentContainer from '../../components/componentContainer';
import Group from '../models/group';
import {
  USER_GROUP_ADMIN,
  USER_GROUP_STUDENT_ASSIST,
  USER_GROUP_TEACHER_ASSIST,
  USER_GROUP_TEACHER_TRAINER,
  USER_GROUP_TECH_SUPPORT,
  USER_GROUP_FINANCE,
  USER_GROUP_TEACHER,
  USER_GROUP_STUDENT,
} from '../common/groups';

componentContainer.init({});

const GROUPS = [
  {
    name: USER_GROUP_ADMIN,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_STUDENT_ASSIST,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_TECH_SUPPORT,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_TEACHER_ASSIST,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_TEACHER_TRAINER,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_FINANCE,
    isCustomer: false,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_STUDENT,
    isCustomer: true,
    members: [],
    responseGroups: [],
  },
  {
    name: USER_GROUP_TEACHER,
    isCustomer: true,
    members: [],
    responseGroups: [],
  },
];

async function ensureGroups() {
  const promises = GROUPS.map(g =>
    Group.findOne({ name: g.name }).then((group) => {
      if (group) return group;
      const newGroup = new Group(g);
      return newGroup.save();
    }));
  return Promise.all(promises);
}

async function ensureResponseGroup(groups) {
  const [
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    student,
    teacher,
  ] = groups;

  // admin
  await admin.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    student,
    teacher,
  );

  await admin.save();

  // student Assist
  studentAssist.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    student,
  );

  await studentAssist.save();
  // tech support
  techSupport.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    student,
  );

  await techSupport.save();
  // teacher Assist
  teacherAssist.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    teacher,
  );

  await teacherAssist.save();
  // teacher trainer
  teacherTrainer.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
    teacher,
  );

  await teacherTrainer.save();
  // finance trainer
  finance.responseGroups.addToSet(
    admin,
    studentAssist,
    techSupport,
    teacherAssist,
    teacherTrainer,
    finance,
  );

  await finance.save();
  // student
  student.responseGroups.addToSet(admin, studentAssist);

  await student.save();
  // teacher
  teacher.responseGroups.addToSet(admin, teacherAssist, teacherTrainer);
  await teacher.save();
}

async function init() {
  const groups = await ensureGroups();
  await ensureResponseGroup(groups);
}

if (!module.parent) {
  // run in script
  init()
    .then(() => {
      console.log('    ########### initialized #########\n');
      process.exit();
    })
    .catch((e) => {
      console.error(e);
      process.exit(1);
    });
}

export default init;
